import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/app/app_article_card.dart';
import 'package:bxultimate/widgets/app/app_shop_activity.dart';
import 'package:bxultimate/widgets/app/app_task_card.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/app/app_work_trend.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:bxultimate/widgets/mk_search_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class HomeIndexPage extends StatefulWidget {
  const HomeIndexPage({Key? key}) : super(key: key);

  @override
  State<HomeIndexPage> createState() => _HomeIndexPageState();
}

class _HomeIndexPageState extends State<HomeIndexPage> {
  final controller = Get.find<HomeIndexController>();
  final state = Get.find<HomeIndexController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
        child: Scaffold(
      backgroundColor: Colors.transparent,
      appBar: MkSearchBar(
        onTap: () {
          Get.toNamed(RouterName.homeSearch);
        },
      ),
      body: EasyRefresh(
        header: TDRefreshHeader(),
        onRefresh: () async {
          controller.getHomeData();
        },
        child: Material(
          color: Colors.transparent,
          child: _buildBody(context),
        ),
      ),
    ));
  }

  _buildHomeTask() {
    return Obx(() => state.homeData.task.length > 0
        ? Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  AppTitle(
                    title: "任务通知",
                    icon: Assets.homeTaskNotification,
                  ),
                  GestureDetector(
                    onTap: () => {Get.toNamed(RouterName.taskList)},
                    child: AppTitle(
                      title: "全部任务",
                      textStyle: TextStyle(color: Color(0xFF606266)),
                      icon: Assets.homeAllTask,
                    ),
                  ),
                ],
              ),
              Gaps.vGap8,
              SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: state.homeData.task
                          .map((e) => AppTaskCard(task: e))
                          .toList())),
              Gaps.vGap12,
            ],
          )
        : SizedBox());
  }

  _buildZqgs() {
    return Obx(() => state.homeData.story.length > 0
        ? Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  AppTitle(
                    title: "支前故事",
                    icon: Assets.homeZqgs,
                  ),
                  GestureDetector(
                    onTap: () {
                      Get.toNamed(RouterName.articleList);
                    },
                    child: AppTitle(
                        title: "查看更多",
                        textStyle: TextStyle(color: Color(0xFF606266),fontSize: 12)),
                  ),
                ],
              ),
              Gaps.vGap8,
              Column(
                children: state.homeData.story
                    .map((e) => AppArticleCard(
                          article: e,
                          onTap: () {
                            controller.toArticleDetail(e);
                          },
                        ))
                    .toList(),
              ),
              Gaps.vGap12,
            ],
          )
        : SizedBox());
  }

  _buildWztj() {
    return Obx(() => state.homeData.article.length > 0
        ? Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  AppTitle(
                    title: "文章推荐",
                    icon: Assets.homeWztj,
                  ),
                  GestureDetector(
                    onTap: () {
                      Get.toNamed(RouterName.articleMoreTabList);
                    },
                    child: AppTitle(
                        title: "查看更多",
                        textStyle: TextStyle(color: Color(0xFF606266),fontSize: 12)),
                  ),
                ],
              ),
              Gaps.vGap8,
              Column(
                children: state.homeData.article
                    .map((e) => AppArticleCard(
                          article: e,
                          onTap: () {
                            controller.toArticleDetail(e);
                          },
                        ))
                    .toList(),
              ),
            ],
          )
        : SizedBox());
  }

  _buildGzdt() {
    return Obx(() => state.homeData.job.length > 0
        ? Column(children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                AppTitle(
                  title: "工作动态",
                  icon: Assets.homeWorkDt,
                ),
                GestureDetector(
                  onTap: () {
                    Get.toNamed(RouterName.workTrendList);
                  },
                  child: AppTitle(
                      title: "查看更多",
                      textStyle: TextStyle(color: Color(0xFF606266),fontSize: 12)),
                ),
              ],
            ),
            Gaps.vGap8,
            Column(
              children:
                  state.homeData.job.map((e) => AppWorkTrend(job: e)).toList(),
            ),
          ])
        : SizedBox());
  }

  _buildShop() {
    return Obx(() => state.homeData.shopActivity.length > 0
        ? Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  AppTitle(
                    title: "商家动态",
                    icon: Assets.homeWorkDt,
                  ),
                  GestureDetector(
                    onTap: () {
                      Get.toNamed(RouterName.shopActivityList);
                    },
                    child: AppTitle(
                        title: "查看更多",
                        textStyle: TextStyle(color: Color(0xFF606266))),
                  ),
                ],
              ),
              Gaps.vGap8,
              Column(
                children: state.homeData.shopActivity
                    .map((e) => AppShopActivity(shopActivity: e))
                    .toList(),
              ),
            ],
          )
        : SizedBox());
  }

  _buildBody(context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Gaps.vGap12,
          _buildHomeTask(),
          _buildZqgs(),
          _buildWztj(),
          _buildGzdt(),
          _buildShop(),
        ],
      ),
    );
  }
}
